package main;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.File;
import java.util.LinkedList;
import java.util.List;
import javax.swing.JFileChooser;
import model.DataSource;
import model.DataTable;
import view.GraphManager;

/**
 * @author Wynne Lok
 */
public class Controller {

    /**
     * Tells whether a key has been pressed or not.
     */
    public static final int NO_KEY_PRESSED = -1;
    private static final JFileChooser OUR_CHOOSER = new JFileChooser(System.getProperties()
                                                                 .getProperty("user.dir"));
    private int myLastKeyPressed;
    private GraphManager myContGraph;
    private File myInputFile;
    private DataSource mySource;

    /**
     * Controls what happens when the user presses a key
     * @param myGraphs is the view
     */
    public Controller(GraphManager myGraphs) {
        //setInputListener(myGraphs);
        myContGraph = myGraphs;
        setInputListener(myContGraph);
    }

    /**
     * Waits for the user to press a key
     * @param m is the view
     */
    private void setInputListener (GraphManager m) {
        myLastKeyPressed = NO_KEY_PRESSED;
        myContGraph = m;
        // add the keyboard listener
        myContGraph.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed (KeyEvent e) {
                myLastKeyPressed = e.getKeyCode();
                manageKeys(myLastKeyPressed);
            }

            @Override
            public void keyReleased (KeyEvent e) {
                myLastKeyPressed = NO_KEY_PRESSED;
            }
        });
    }

    private void manageKeys (int keyCode) {
        switch (keyCode) {

            case KeyEvent.VK_C:
                getNewData();
                myContGraph.repaint();
                break;
            default:
                myContGraph.toggleForce(keyCode);
                break;
        }
    }

    /**
     * Allows user to input new data.
     */
    public void getNewData () {
        myInputFile = null;
        int response = OUR_CHOOSER.showOpenDialog(null);
        if (response == JFileChooser.APPROVE_OPTION) {
            myInputFile = OUR_CHOOSER.getSelectedFile();
        }
        mySource = new DataTable();
        mySource.loadDataFromFile(myInputFile);

        List<String> dataColumns = mySource.getColumnNames();
        List<String> inputData = mySource.getColumn(dataColumns.get(1));
        List<Object> data = new LinkedList<Object>();
        data.add(dataColumns.get(1));
        for (String number : inputData) {
            data.add(Integer.parseInt(number));
        }
        inputData = mySource.getColumn(dataColumns.get(1));
        List<Object> xdata = new LinkedList<Object>();
        for (String number : inputData) {
            xdata.add(Integer.parseInt(number));
        }
        myContGraph.setData(data);
    }

    /**
     * Sets the current main as the main being used.
     * 
     * @param manager is the view being used
     */
    public void setController (GraphManager manager) {
        myContGraph = manager;
    }
}

